<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>
    <div style="margin-top: 3px; text-align: right">
      <el-button type="primary" @click="subMit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRolesListApi } from '@/api/settings'
import { getUserDetailByIdApi } from '@/api/user'
import { assignRolesApi } from '@/api/employees'
export default {
  props: {
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      paramsObj: {
        page: 1,
        size: 90
      },
      list: [],
      checkList: []
    }
  },
  watch: {},
  created() {
    this.getEmployeeList() // 获取所有角色
  },
  methods: {
    // 弹出框的确认按钮
    async subMit() {
      await assignRolesApi({
        id: this.id,
        roleIds: this.checkList
      })
      // 请求成功后关闭对话框
      this.$emit('closeDialog')
    },
    // 弹出框的取消按钮
    closeDialog() {
      this.$emit('closeDialog')
    },
    // 获取所有角色
    async getEmployeeList() {
      const res = await getRolesListApi(this.paramsObj)
      this.list = res.data.rows
      const res1 = await getUserDetailByIdApi(this.id)
      this.checkList = res1.data.roleIds
    }
  }
}
</script>
<style lang="scss" scoped>
.el-checkbox {
  margin-bottom: 20px;
}
</style>
